<template>
    <div>
      <!-- <wbc-nav ref="child"></wbc-nav> -->
      <el-card style="width: 100%; margin: 0 auto; padding: 50px; margin-bottom: 100px">
        <div class="app-container common-list-page" style="width: 500px; margin: 0 auto">
          <h1 style="text-align: center; margin-bottom: 50px;">当前用户为：{{user.nickname}}</h1>
          <el-form
              :model="resetForm"
              :rules="resetFormRules"
              ref="resetForm"
              status-icon
              label-width="100px"
          >
            <el-form-item label="旧密码：" prop="password">
              <el-input type="password" v-model="resetForm.password" auto-complete="off"></el-input>
            </el-form-item>
            <el-form-item label="新密码：" prop="newpwd">
              <el-input type="password" v-model="resetForm.newpwd" auto-complete="off"></el-input>
            </el-form-item>
            <el-form-item label="确认密码：" prop="newpassword1">
              <el-input type="password" v-model="resetForm.newpassword1" auto-complete="off"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="toAmend">确认修改</el-button>
            </el-form-item>
          </el-form>
        </div>
      </el-card>
    </div>
  
  </template>
  
  <script>
  // import header from '../views/header'
  export default {
    name: "changepasswoder",
    components:{
      // 'wbc-nav':header,
    },
    data() {
      var validatePass = (rule, value, callback) => {
        if (!value) {
          callback(new Error("请输入新密码"));
        } else if (value.toString().length < 6 || value.toString().length > 18) {
          callback(new Error("密码长度为6-18位"));
        } else {
          callback();
        }
      };
      var validatePass2 = (rule, value, callback) => {
        if (value === "") {
          callback(new Error("请再次输入密码"));
        } else if (value !== this.resetForm.newpwd) {
          callback(new Error("两次输入密码不一致!"));
        } else {
          callback();
        }
      };
      return {
        user: JSON.parse(localStorage.getItem("user")===""?{}:localStorage.getItem("user")),
        resetForm: {
          //传给后台所需要的参数
          newpassword1: "",
          password: "",
          username: ""//此处只是后台需要的字段而已，如果前期有公用cookie里面有获取并且保存过，现在需要另外调用进来，具体的获取方法就看个人了
        },
        resetFormRules: {
          password: [
            { required: true, message: "请输入旧密码", trigger: 'blur' }
          ],
          newpwd: [
            { required: true, validator: validatePass, trigger: 'blur' }
          ],
          newpassword1: [
            { required: true, validator: validatePass2, trigger: "blur" }
          ]
        }
      };
    },
  
    mounted() {
      console.log(this.user)
    },
    methods:{
      //提交修改的密码
      toAmend(){
  
        if(this.user.password === this.resetForm.password && this.resetForm.newpassword1 !== "" ){
          this.user.password = this.resetForm.newpassword1;
            this.request.post("/user",this.user).then(res=>{
              if(res.code === "200"){
                this.$message.success("恭喜你修改成功，三秒后跳转到登录")
                this.openFullScreenSetRoleMenu()
  
              }
            })
        }else {
          this.$message.error("请确认后重新修改");
        }
  
      },
      openFullScreenSetRoleMenu() {
        const loading = this.$loading({
          lock: true,
          text: '正在前往登录页面.....',
          spinner: 'el-icon-loading',
          background: 'rgba(0, 0, 0, 0.2)'
        });
        setTimeout(() => {
          this.$store.commit("logoutOn");
          loading.close();
        }, 1500);
      }
    }
  }
  </script>
  
  <style scoped>
  
  </style>